<template>
  <div v-loading="loading" class="container">
   
    <el-row :gutter="30">
      <el-col :span="12">
        <el-card class="box-card" ref="baseRef">
      <div slot="header" class="clearfix" >
        <span>基础资料</span>
      </div>
      <el-descriptions class="margin-top" :column="2" :size="'large'" border direction="vertical">
    <el-descriptions-item>
      <template slot="label">
        <i class="el-icon-user"></i>
        学校名称
      </template>
      <el-tag size="small">{{formData.uniName}}</el-tag>
    </el-descriptions-item>
    <el-descriptions-item>
      <template slot="label">
        <i class="el-icon-location-outline"></i>
        城市
      </template>
      <el-tag size="small" style="margin-right: 4px;">{{formData.city}}</el-tag>
    </el-descriptions-item>
    <el-descriptions-item>
      <template slot="label">
        <i class="el-icon-tickets"></i>
        招生类型
      </template>
      <div style="max-width:400px">
        <el-tag effect="dark" size="small" style="margin-right:4px;margin-top:4px;">{{formData.enrollmentTypeInfo}}</el-tag>
      </div>
    </el-descriptions-item>
    <el-descriptions-item>
      <template slot="label">
        <i class="el-icon-tickets"></i>
        层次
      </template>
      <div style="max-width:400px">
        <el-tag effect="dark" :type="getType()" size="small" v-for="(item,index) in formData.tagList" :key="index" style="margin-right:4px;margin-top:4px;">{{item}}</el-tag>
      </div>
    </el-descriptions-item>
    <el-descriptions-item>
      <template slot="label">
        <i class="el-icon-tickets"></i>
        院校标识
      </template>
      <div style="max-width:400px">
        <!-- <el-tag effect="dark" :type="getType()" size="small" v-for="(item,index) in formData.tagList" :key="index" style="margin-right:4px;margin-top:4px;">{{item}}</el-tag> -->
      </div>
    </el-descriptions-item>
    <el-descriptions-item>
      <template slot="label">
        <i class="el-icon-tickets"></i>
        年份
      </template>
      <div style="max-width:400px">
        <!-- <el-tag effect="dark" :type="getType()" size="small" v-for="(item,index) in formData.tagList" :key="index" style="margin-right:4px;margin-top:4px;">{{item}}</el-tag> -->
      </div>
    </el-descriptions-item>
    <el-descriptions-item>
      <template slot="label">
        <i class="el-icon-tickets"></i>
        师资力量
      </template>
      <div>
        {{ formData.teacherResource }}
      </div>
    </el-descriptions-item>
    <el-descriptions-item>
      <template slot="label">
        <i class="el-icon-tickets"></i>
        学科实力
      </template>
      <div >
        {{formData.subjectLevel}}
      </div>
    </el-descriptions-item>
    <el-descriptions-item>
      <template slot="label">
        <i class="el-icon-tickets"></i>
        学位授权点
      </template>
      <div >
        {{ formData.degreePoints }}
      </div>
    </el-descriptions-item>
    <el-descriptions-item>
      <template slot="label">
        <i class="el-icon-tickets"></i>
        特殊规定
      </template>
      <div >
        {{ formData.specialRequirements }}
      </div>
    </el-descriptions-item>
    <el-descriptions-item>
      <template slot="label">
        <i class="el-icon-tickets"></i>
        录取原则
      </template>
      <div >
        {{formData.admissionPrinciple}}
      </div>
    </el-descriptions-item>
    <el-descriptions-item>
      <template slot="label">
        <i class="el-icon-tickets"></i>
        录取分数线说明
      </template>
      <div >
        {{formData.admissionLineDsc}}
      </div>
    </el-descriptions-item>
    <el-descriptions-item>
      <template slot="label">
        <i class="el-icon-coordinate"></i>
        支持学位
      </template>
      <el-tag size="small" v-if="formData.degreeNameList && formData.degreeNameList.length" style="margin-right:10px">{{formData.degreeNameList.join(',')}}</el-tag>
    </el-descriptions-item>
  </el-descriptions>
  
    </el-card>
      </el-col>
      <el-col :span="12">
        
    <el-card class="box-card" :style="`height:${baseHeight}px`">
      <div slot="header" class="clearfix">
        <div class="flex_bet" style="height: 80%;">
          <span>院校概况</span>
              <!-- <el-button v-if="!isIntroEdit" class="btn" icon="el-icon-edit" type="primary" size="mini" @click="isIntroEdit = true"></el-button>
              <el-button v-else class="btn"  type="primary" size="mini" @click="confirm">提交</el-button> -->
        </div>
      </div>
      <span v-if="!isIntroEdit">{{ formData.intro }}</span>
      <el-input v-else type="textarea" v-model="formData.intro" :rows="6" placeholder="请输入大学简介"></el-input>
    </el-card>
      </el-col>
    </el-row>
    
    <el-row :gutter="30">
      <el-col :span="12">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>校徽</span>
      </div>

      
      <el-upload
          v-loading="emblem_loading"
          class="avatar-uploader el-upload"
          action="https://jsonplaceholder.typicode.com/posts/"
          :http-request="(params)=>uploadRequest(params,'emblem')"
          :show-file-list="false"
        >
          <img v-if="formData.emblem" :src="formData.emblem" class="avatar">
          <div v-else class="upload-text">选择图片</div>
        </el-upload>
    </el-card>
    
  </el-col>
      <el-col :span="12">

        <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>背景图</span>
      </div>
      
      <el-upload
          v-loading="bgImgUrl_loading"
          class="avatar-uploader el-upload"
          action="https://jsonplaceholder.typicode.com/posts/"
          :http-request="(params)=>uploadRequest(params,'bgImgUrl')"
          :show-file-list="false"
        >
          <img v-if="formData.bgImgUrl" :src="formData.bgImgUrl" class="avatar">
          <div v-else class="upload-text">选择图片</div>
        </el-upload>
    </el-card>
        </el-col>
    </el-row>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <!-- teacherResource -->
        <div class="flex_bet">
          <span>师资力量</span>
              <el-button class="btn" icon="el-icon-plus" type="primary" size="mini" @click="addTeacher()"></el-button>
        </div>
      </div>
      
    <el-table
      :data="tableData"
      style="width: 100%">
      <el-table-column
        label="头像"
        >
        <template slot-scope="scope">
          <el-image fit="cover" class="avator" lazy :src="scope.row.bgImgUrl"></el-image>
        </template>
      </el-table-column>
      <el-table-column
        prop="name"
        label="姓名"
        >
      </el-table-column>
          <el-table-column label="操作" align="center" width="270">
            <template slot-scope="scope">
              <el-button class="btn" size="mini" icon="el-icon-edit" type="primary" @click="edit(scope.row,scope.$index)"></el-button>
              <el-button class="btn" size="mini" icon="el-icon-delete" type="primary" @click="del(scope.row,scope.$index)"></el-button>
            </template>
          </el-table-column>
    </el-table>
    </el-card>
    <div>
         <el-button class="btn" type="primary" size="mini" @click="confirm" style="padding: 20px 60px;font-size: 18px;margin: 0 auto;display: block;">保存</el-button>
    </div>
    <addTeacherDialog ref="addTeacherDialog" @ok="addTeacherOk"></addTeacherDialog>
  </div>
</template>

<script>
import { uploadFile } from '@/api/admin.js'
import { getInfoById, add } from '@/api/web/school'
  import addTeacherDialog from './modual/addTeacherDialog';
export default {
  name:'schoolAdd',
  components: { addTeacherDialog },
  data() {
    return {
      submitLoading: false,
      loading: false,
      formData: {},
      img_loading: false,
      emblem_loading:false,
      bgImgUrl_loading:false,
      tableData: [],
      dialogVisible: false,
      baseHeight:230,
      isIntroEdit:false,//简介B级
      currentEditIndex:0,
      rules: {
        bannerImage: [{
          required: true,
          message: '请上传封面图片'
        }],
        content: [{
          required: true,
          message: '请输入新闻内容'
        }],
        content: [{
          required: true,
          message: '请输入英文版内容'
        }],
        title: [{
          required: true,
          message: '请输入新闻标题'
        }],
        titleEn: [{
          required: true,
          message: '请输入英文标题'
        }]
      }
    }
  },
  mounted() {
    this.$nextTick(()=>{
      this.baseHeight = this.$refs.baseRef.$el.scrollHeight - 20
    })
    if (this.$route.query.id) {
      this.getInfoById()
    }
  },
  methods: {
    getInfoById() {
      this.loading = true
      getInfoById({ uniId: +this.$route.query.id }).then(res => {
        this.loading = false
        if (res.code == 200) {
          this.formData = res.data
          if (this.formData.teacherPhotoList && this.formData.teacherPhotoList.length) {
            let sources = this.formData.teacherResource.split(',')
            this.formData.teacherPhotoList.forEach((val,index)=>{
              this.tableData.push({
                name:sources[index],
                bgImgUrl:val
              })
            })
          }
        } else {
          this.$message({
            type: 'error',
            message: res.msg
          })
        }
      })
    },
    uploadRequest(params) {
      this.img_loading = true
      uploadFile(params).then(res => {
        this.img_loading = false
        if (res && res.data && res.data.length) {
          this.$set(this.formData, 'cover', res.data[0])
        }
      })
    },
    
    addTeacher(){
        this.$refs.addTeacherDialog.add()
    },
    addTeacherOk(data){
      if(data.action === 'edit'){
        this.tableData.splice(this.currentEditIndex,1,data.data)
      }else{
      this.tableData.push(data.data)
      }
      console.log('this.tableData :>> ', this.tableData);
    },
    edit(row,index){
      this.currentEditIndex = index
      this.$refs.addTeacherDialog.edit(row)
    },
    del(row,index){
      this.tableData.splice(index,1)
    },
    getType(){
      let arr = ['success','info','warning','danger']
            // 1. 0~1之间的随机数
            var num = Math.random();
            // 2. 0~10 之间的随机数
            var num1 = num * 4;
            // 3. 0~10 之间的 随机整数
            var num2 = Math.round( num1 );
      return arr[num2 - 1]
    },
      /**
       * 保存
       */
       confirm(closeDialog){
        this.formData.uniId = +this.$route.query.id
        this.formData.teacherPhotoList = this.tableData.map(v=>v.bgImgUrl)
        this.formData.teacherResource = this.tableData.map(v=>v.name).join(',')
        add(this.formData).then(res => {
          this.submitLoading = false
          if (res.code == 200) {
            this.$message({
              type: 'success',
              message: '提交成功'
            })
            this.$emit('ok')
            this.visible = false;
          } else {
            this.$message({
              type: 'error',
              message: res.message
            })
          }
        })
      },
  }
}
</script>

<style scoped>
.avatar-uploader,.el-upload {
    border: 1px solid #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    width: 178px!important;
    height: 178px;
  }
  .avatar-uploader:hover,.el-upload:hover {
    border-color: #409EFF;
  }
  .upload-text {
    font-size: 14px;
    color: #8c939d;
    line-height: 178px;
    text-align: center;
    margin: 0 auto;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
  .container{
    padding-bottom: 30px;
    padding-top: 20px;
  }
.box-card label{
  color: #999;
}
.box-card {
  margin-bottom: 20px;
  min-height: 230px;
}
.img{
  width: 150px;
  height: 150px;
  object-fit: cover;
  margin-right: 10px;
}
.img img{
  object-fit: cover !important;;
}
.flex_bet{
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.avator{
  width: 80px;
  height: 80px;
}
</style>
<style>
  .demo-ruleForm .el-input,.demo-ruleForm .el-select{
    width: 300px;
  }

.img img{
  object-fit: cover !important;
}
</style>
